package com.ltx.controller;

import com.ltx.pojo.Record;
import com.ltx.pojo.Week;
import com.ltx.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.security.provider.Sun;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Record")
public class RecordController {

    public final RecordService recordService;
    public RecordController(@Qualifier("recordServiceImpl") RecordService recordService) {
        this.recordService = recordService;
    }

    //查询所有人的训练记录
    @RequestMapping("allRecord")
    public String allRecord(Model model){
        List<Record> records = recordService.allRecord();
        Week week = new Week();
        for (Record record : records) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(record.getStartTime());
            // 指示一个星期中的某天,0代表星期天。
            int w = Math.max(cal.get(Calendar.DAY_OF_WEEK) - 1, 0);
            System.out.println(w);
            if(w==0){week.Sun++;}else if(w==1){week.Mon++;}else if(w==2){week.Tue++;}else if(w==3){week.Wed++;}else if(w==4){week.Thu++;}else if(w==5){week.Fri++;}else if(w==6){week.Sat++;};
        }
        model.addAttribute("Mon",week.Mon);
        model.addAttribute("Tue",week.Tue);
        model.addAttribute("Wed",week.Wed);
        model.addAttribute("Thu",week.Thu);
        model.addAttribute("Sat",week.Sat);
        model.addAttribute("Fri",week.Fri);
        model.addAttribute("Sun",week.Sun);
        model.addAttribute("list",records);
        return "/allRecord";
    }
    //登记开始训练时间
    @RequestMapping("/toStartRecord")
    public String toStartRecord(Model model){
        Date date = new Date();
        model.addAttribute("date",date);
        return "/startRecord";
    }
    @RequestMapping("/startRecord")
    public String startRecord(Record record){
        System.out.println(record);
        recordService.startRecord(record);
        return "redirect:/Record/allRecord";
    }
     //根据会员id查询会员训练记录
      @RequestMapping("queryRecord")
       public  String memberRecord(int mId,Model model){
          List<Record> records1 = recordService.memberRecord(mId);
          List<Record> records=new ArrayList<>();
          records=records1;
       if(records1.isEmpty()){
          records=recordService.allRecord();
               model.addAttribute("error","未查询到");
       }
       model.addAttribute("list",records);
             return "allRecord";
        }
    //结束训练
    @RequestMapping("/overRecord")
    public  String overRecord(Record record) {
        Date date = new Date();
        record.setOverTime(date);
        System.out.println(record);
        recordService.overRecord(record);
        return "redirect:/Record/allRecord";
    }
}
